package ventureco.communication;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class AgentMessage implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	//the conditions that are going to be used
	public boolean newTasks = false;
	public boolean taskIsTaken = false;
	public boolean taskComplated = false;
	public boolean taskIsFailed = false;
	public boolean allArea = false;
	
	public static String myAddress;
	
	//to send messages in network messages are converted to byte
		public byte[] convertToByte()
		{
			
			ByteArrayOutputStream baos = new ByteArrayOutputStream();

			ObjectOutputStream objout;
			try {
				objout = new ObjectOutputStream(baos);
				objout.writeObject(this);
			} catch (IOException e) {
				
				e.printStackTrace();
			}
			
			return baos.toByteArray();
		}
		


		public void setNewTasks(boolean newTasks) {
			this.newTasks = newTasks;
		}


		public void setTaskIsTaken(boolean taskIsTaken) {
			this.taskIsTaken = taskIsTaken;
		}


		public void setTaskComplated(boolean taskComplated) {
			this.taskComplated = taskComplated;
		}

		public void setTaskIsFailed(boolean taskIsFailed) {
			this.taskIsFailed = taskIsFailed;
		}

		public void setAllArea(boolean allArea) {
			this.allArea = allArea;
		}

		//to get agent messages from network bytes are converted to Arrays	
		public static AgentMessage createFromByteArray(byte[] stream)
		{
			ByteArrayInputStream baos = new ByteArrayInputStream(stream);

			ObjectInputStream objin;
			try {
				objin = new ObjectInputStream(baos);
				AgentMessage atm = (AgentMessage) objin.readObject();
				return atm;
			} catch (IOException e) {
				
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				
				e.printStackTrace();
			}
			
			return null;

		}

}
